package com.entrogy.controller;

import com.entrogy.service.AttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "学校作息时间设置")
@RestController
@RequestMapping(value = "/attendance")
public class AttendanceController {

    private static final Logger logger = LoggerFactory.getLogger(AttendanceController.class);

    @Autowired
    private AttendanceService attendanceService;

    @ApiOperation(value = "获取当天的时间表(看是否存在某天的时间表，有的话就返回)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "用户ID",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "arrangeDate",value = "日期",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "schoolCode",value = "学校编码",dataType = "query",paramType = "int",required = true)
    })
    @RequestMapping(value = "getdynarrange",method = RequestMethod.POST)
    public JSONObject getDynArrange(String uid, String arrangeDate,int schoolCode) {
        JSONObject jsonObject = attendanceService.getDynArrange(uid, arrangeDate,schoolCode);
        return jsonObject;
    }

    @ApiOperation(value = "增加某天的时间表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "用户ID",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "arrangeDate",value = "日期",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "schoolCode",value = "学校编码",dataType = "query",paramType = "int",required = true)
    })
    @RequestMapping(value = "adddynarrange",method = RequestMethod.POST)
    public JSONObject addDynArrange(String uid,String arrangeDate,int schoolCode) {
        JSONObject jsonObject = attendanceService.addDynArrange(uid, arrangeDate,schoolCode);
        return jsonObject;
    }

    @ApiOperation(value = "更新某一天的时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "用户ID",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "arrangeDate",value = "所选日期",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "gradelist",value = "日期列表",dataType = "query",paramType = "String",required = true),
            @ApiImplicitParam(name = "schoolCode",value = "学校编码",dataType = "query",paramType = "int",required = true)
    })
    @RequestMapping(value = "updatedynarrange",method = RequestMethod.POST)
    public JSONObject updateDynArrange(String uid,String arrangeDate,String gradelist,int schoolCode) {
            JSONArray classListArr = JSONArray.fromObject(gradelist);
            JSONObject jsonObject = attendanceService.updateDynArrange(uid, arrangeDate, classListArr,schoolCode);
            jsonObject.put("gradelist", attendanceService.getDynArrange(uid, arrangeDate,schoolCode).getJSONArray("gradelist"));
            return jsonObject;
    }
}
